#include "BaseLauncher.h"
#include "BtOgrePG.h"
#include "BtOgreGP.h"
#include "BtOgreExtras.h"
#include "OgreConsole.h"
#include <Ogre.h>
#include <OgreVector3.h>
#include <OgreMath.h>
#include "BaseProjectile.h"
#include "ProjectileManager.h"
#include "GameState.hpp"
#include "PhysicsManager.h"
#include "DefaultCube.h"

BaseLauncher::BaseLauncher(Ogre::String name, Ogre::SceneManager* sceneMgr, Ogre::SceneNode* parentNode, Ogre::Vector3 pPos, Ogre::Quaternion orientation)
{
	OgreConsole::getSingleton().print("Launcher created");
	m_pSceneMgr = sceneMgr;
	m_ParentNode = parentNode;

	m_LauncherEntity =  m_pSceneMgr->createEntity(name, "base_cannon.mesh");
	m_LauncherEntity ->setMaterialName("Flat");
	m_LauncherNode = m_ParentNode->createChildSceneNode(name + "Launcher node");

	m_LauncherNode->attachObject(m_LauncherEntity);
	m_LauncherNode->scale(Vector3(0.75, 0.75, 0.75));
	m_LauncherNode->setPosition(pPos);
	m_LauncherNode->setOrientation(orientation);

	m_CanFireDelay = 40;
	m_CanFire = true;

	m_LauncherTimer = new Ogre::Timer(); 
	//Fire line
	m_FireLineCube = new DefaultRectangle("SimpleCubeO1" + name, Vector3(0.1, 0 , 5), Ogre::ColourValue(1.0,0.0,0.0,1.0));
	//m_FireLineCube->setCastShadows(false);
	MeshPtr mesh;
	mesh = m_FireLineCube->convertToMesh("LineMesh" + name);
	//mesh->buildEdgeList();
   // mesh->prepareForShadowVolume();

	m_LineEntity =  m_pSceneMgr->createEntity("LineEntity" +name, "LineMesh" + name);	
	m_FireLineNode = m_LauncherNode->createChildSceneNode();
	m_FireLineNode->attachObject(m_LineEntity);
    m_FireLineNode->setPosition(Vector3(0.05, 0, -5));
}


BaseLauncher::~BaseLauncher(void)
{
}

void BaseLauncher::updateLauncher(double timeSinceLastFrame){


	if(!m_CanFire){
		   if(m_LauncherTimer->getMilliseconds() > m_CanFireDelay){
				m_CanFire = true;
		   }
	   }
}

void BaseLauncher::updateTarget(Ogre::Vector3 target)
{
	m_LauncherTarget = target;
}

void BaseLauncher::fireLauncher(){

	if(!m_CanFire){
		return;
	}

	ProjectileManager::getSingletonPtr()->addBaseMissileFromCache(m_LauncherNode->_getDerivedPosition(), m_LauncherNode->_getDerivedOrientation(), m_LauncherTarget);
	m_CanFire = false;
	m_LauncherTimer->reset();
}

void BaseLauncher::changeDebugVisibility(){

	m_FireLineNode->flipVisibility();

}
